/*
 * @(#)DeviceResolveInterceptor.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hangame.mbl.constant.DeviceIdConst;
import com.nhncorp.lucy.common.data.DataHandlerFactory;
import com.nhncorp.lucy.web.context.ServiceContext;
import com.nhncorp.lucy.web.interceptor.AbstractInterceptor;
import com.opensymphony.webwork.WebWorkStatics;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

// TODO: Auto-generated Javadoc
/**
 * user-agent를 보고 어떤 디바이스인지 resolve 한다.
 * 
 * @author khpark
 */
public class DeviceResolveInterceptor extends AbstractInterceptor implements Interceptor, WebWorkStatics {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 4028193134673416622L;
	
	/** The log. */
	private static Log log = LogFactory.getLog(DeviceResolveInterceptor.class);
	
	
	/** The Constant URL_MOBILE_SMART. */
	private static final String URL_MOBILE_SMART = DataHandlerFactory.getDataHandler().get("url/mobile_smart");
	
	/** The Constant URL_MOBILE_HANGAME. */
	private static final String URL_MOBILE_HANGAME = DataHandlerFactory.getDataHandler().get("url/mobile_hangame");

	/* (non-Javadoc)
	 * @see com.nhncorp.lucy.web.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork.ActionInvocation)
	 */
	public String intercept(ActionInvocation invocation) throws Exception {
		String userAgent = ServiceContext.getHeader("User-Agent");
		
//		String referer = ServiceContext.getReferer();
//		boolean isRedirectedFromMobile = StringUtils.contains(referer, URL_MOBILE_SMART) || StringUtils.contains(referer, URL_MOBILE_HANGAME); //모바일 사이트에서 redirect되어 유입된 경우인지
		
		String deviceType = "";

		if (StringUtils.contains(userAgent, "iPhone")) {
			deviceType = DeviceIdConst.IPHONE;
		} else if (StringUtils.contains(userAgent, "SCH-I800")) { //갤럭시탭
			deviceType = DeviceIdConst.GALAXYTAB;
		} else if (StringUtils.contains(userAgent, "Android")) { //안드로이드류
			deviceType = DeviceIdConst.ANDROID;
		} else if (StringUtils.contains(userAgent, "iPad")) {
			deviceType = DeviceIdConst.IPAD;
		} else {
			deviceType = DeviceIdConst.ANOTHER;
		}

		boolean isPadType = DeviceIdConst.GALAXYTAB.equals(deviceType) || DeviceIdConst.IPAD.equals(deviceType); //PAD류 기기 인경우

		log.debug("+++DEVICE_TYPE : " + deviceType);

		HttpServletRequest request = (HttpServletRequest)invocation.getInvocationContext().get(HTTP_REQUEST);
		request.setAttribute("DEVICE_TYPE", deviceType);
		request.setAttribute("IS_PAD_TYPE", isPadType);
//		request.setAttribute("IS_REDIRECTED_FROM_MOBILE", isRedirectedFromMobile);

		if (StringUtils.contains(userAgent, "MSIE")) { //브라우저가 MS IE인지 여부.
			request.setAttribute("IS_MS_IE", "true");
		}

		return invocation.invoke();
	}
}
